package com.orange.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthSchoolService;
import com.orange.handler.AuthStudentDataService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.bean.StudentData;
import com.orange.modules.entity.AuthStudentSubjectMap;
import com.orange.modules.entity.school.AuthSchool;
import com.orange.modules.entity.school_data.AuthStudentData;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/studentDate")
public class AuthStudentDataController extends BaseController{
	
	private static final Logger log = Logger.getLogger(AuthStudentDataController.class);

	
	@Autowired
	private AuthStudentDataService authStudentDataService;
	
	@Autowired
	private AuthSchoolService authSchoolService;
	
	/**
	 * 新增学生数据
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertStudentData" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertStudentData(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try{
			String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==sid){
				return toResponse(null,ReturnCode.NOLOGIN,"未登陆");
			}
			String subject = (String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);//学科
			String graduatedStudents = map.get("graduatedStudents"); //本专业已毕业学生数
			
			String graduatedUndergraduateMajor = map.get("graduatedUndergraduateMajor");
			String graduatedSpecializedSubject = map.get("graduatedSpecializedSubject");
			String schoolAllStudents = map.get("schoolAllStudents");//全校（院）在校学生数
			String allUndergraduateMajor = map.get("allUndergraduateMajor");
			String allSpecializedSubject = map.get("allSpecializedSubject");
			String inschoolStudents = map.get("inschoolStudents");//本专业在校学生数
			String inschoolUndergraduateMajor = map.get("inschoolUndergraduateMajor");
			String inschoolSpecializedSubject = map.get("inschoolSpecializedSubject");
			String inschoolboy = map.get("inschoolboy");
			String inschoolgirl = map.get("inschoolgirl");
			String teacherQualificationCertificate = map.get("teacherQualificationCertificate");//上年应届生获得相关教师资格证书比例
			String relevantEducationalInstitutions = map.get("relevantEducationalInstitutions");//上年毕业生到相关教育机构初次就业率
			String employingUnits = map.get("employingUnits");//上年毕业生跟踪调查用人单位满意率

			String subjectname = map.get("subjectname");
			String grade = map.get("grade");
			String gradeNumOne = map.get("gradeNumOne");
			String gradeNumTwo = map.get("gradeNumTwo");
			String gradeNumThree = map.get("gradeNumThree");
			String gradeNumFour = map.get("gradeNumFour");
			String wholeNum = map.get("wholeNum");
			String remark1 = map.get("remark1");
			String remark2 = map.get("remark2");
			String remark3 = map.get("remark3");
			String remark4 = map.get("remark4");
			String remark5 = map.get("remark5");
			String remark6 = map.get("remark6");
			String remark7 = map.get("remark7");
			String remark8 = map.get("remark8");
			String remark9 = map.get("remark9");
			String remark10 = map.get("remark10");
			String remark11 = map.get("remark11");
			String remark12 = map.get("remark12");
			String remark13 = map.get("remark13");
			String remark14 = map.get("remark14");
			String status ="0";
			if(graduatedStudents==null || StringUtils.isBlank(graduatedStudents))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业已毕业学生数不能为空");
			if(graduatedUndergraduateMajor==null || StringUtils.isBlank(graduatedUndergraduateMajor))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业已毕业学生数(本科专业)不能为空");
			if(graduatedSpecializedSubject==null || StringUtils.isBlank(graduatedSpecializedSubject))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业已毕业学生数(专科专业)不能为空");
			if(schoolAllStudents==null || StringUtils.isBlank(schoolAllStudents))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"全校（院）在校学生数不能为空");
			if(allUndergraduateMajor==null || StringUtils.isBlank(allUndergraduateMajor))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"全校（院）在校学生数(本科专业)不能为空");
			if(allSpecializedSubject==null || StringUtils.isBlank(allSpecializedSubject))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"全校（院）在校学生数(专科专业)不能为空");
			if(inschoolStudents==null || StringUtils.isBlank(inschoolStudents))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业在校学生数不能为空");
			if(inschoolUndergraduateMajor==null || StringUtils.isBlank(inschoolUndergraduateMajor))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业在校学生数(本科专业)不能为空");
			if(inschoolSpecializedSubject==null || StringUtils.isBlank(inschoolSpecializedSubject))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业在校学生数(本科专业)不能为空");
			if(inschoolboy==null || StringUtils.isBlank(inschoolboy))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校Id不能为空(男生)不能为空");
			if(inschoolgirl==null || StringUtils.isBlank(inschoolgirl))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校Id不能为空(女生)不能为空");
			if(teacherQualificationCertificate==null || StringUtils.isBlank(teacherQualificationCertificate))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"上年应届生获得相关教师资格证书比例不能为空");
			if(relevantEducationalInstitutions==null || StringUtils.isBlank(relevantEducationalInstitutions))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"上年毕业生到相关教育机构初次就业率不能为空");
			if(employingUnits==null || StringUtils.isBlank(employingUnits))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"上年毕业生跟踪调查用人单位满意率不能为空");
			
			if(subjectname==null || StringUtils.isBlank(subjectname))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学科名称不能为空");
			if(grade==null || StringUtils.isBlank(grade))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"年级不能为空");
			if(gradeNumOne==null || StringUtils.isBlank(gradeNumOne))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学生数不能为空");
			if(gradeNumTwo==null || StringUtils.isBlank(gradeNumTwo))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学生数不能为空");
			if(gradeNumThree==null || StringUtils.isBlank(gradeNumThree))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学生数不能为空");
			if(gradeNumFour==null || StringUtils.isBlank(gradeNumFour))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学生数不能为空");
			if(wholeNum==null || StringUtils.isBlank(wholeNum))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学生总数不能为空");
			String nowTime= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			String remark = remark1+"#"+remark2+"#"+remark3+"#"+remark4+"#"+remark5+"#"+remark6+"#"+remark7+"#"+remark8+"#"+remark9+"#"+remark10+"#"+remark11+"#"+remark12+"#"+remark13+"#"+remark14;
			AuthStudentData asd = new AuthStudentData();
			asd.setAllSpecializedSubject(allSpecializedSubject);
			asd.setAllUndergraduateMajor(allUndergraduateMajor);
			asd.setCreatetime(nowTime);
			asd.setEmployingUnits(employingUnits);
			asd.setGraduatedSpecializedSubject(graduatedSpecializedSubject);
			asd.setGraduatedStudents(graduatedStudents);
			asd.setGraduatedUndergraduateMajor(graduatedUndergraduateMajor);
			asd.setInschoolboy(inschoolboy);
			asd.setInschoolgirl(inschoolgirl);
			asd.setInschoolSpecializedSubject(inschoolSpecializedSubject);
			asd.setInschoolStudents(inschoolStudents);
			asd.setInschoolUndergraduateMajor(inschoolUndergraduateMajor);
			asd.setLogintype(subject);
			asd.setRelevantEducationalInstitutions(relevantEducationalInstitutions);
			asd.setSchoolAllStudents(schoolAllStudents);
			asd.setSid(sid);
			asd.setTeacherQualificationCertificate(teacherQualificationCertificate);
			asd.setStatus(status);
			asd.setRemark(remark);
			authStudentDataService.insertAuthStudentData(asd);
			if(asd.getPid()!=null){
				String[] subjectnameList = subjectname.split(",");
				String[] gradeNumOneList = gradeNumOne.split(",");
				String[] gradeNumTwoList = gradeNumTwo.split(",");
				String[] gradeNumThreeList = gradeNumThree.split(",");
				String[] gradeNumFourList = gradeNumFour.split(",");
				String[] wholeNumList = wholeNum.split(",");
				String[] gradeList = grade.split(",");
				
				for(int i = 0;i<subjectnameList.length;i++){
					AuthStudentSubjectMap assm	=  new AuthStudentSubjectMap();
					assm.setCreatetime(nowTime);
					assm.setGradeNumOne(gradeNumOneList[i]);
					assm.setGradeNumTwo(gradeNumTwoList[i]);
					assm.setGradeNumThree(gradeNumThreeList[i]);
					assm.setGradeNumFour(gradeNumFourList[i]);
					assm.setSdid(asd.getPid());
					assm.setSubjectname(subjectnameList[i]);
					assm.setWholeNum(wholeNumList[i]);
					authStudentDataService.insertAuthStudentSubjectMap(assm);
				}
			}
			AuthSchool as = authSchoolService.queryAuthSchoolInfoBypid(sid);
			asd.setLogintype(subject);
			
			if("1".equals(status)){
				if("1101".equals(subject) ){ //如果是理科
					if(as.getScience().equals("2"))  //如果是登陆过 
						as.setScience("4");
					if(as.getScience().equals("3"))   //如果填写过基础数据状态表
						as.setScience("5");
				}
				if("1102".equals(subject) ){ //如果是文科
					if(as.getLiberalarts().equals("2"))  //如果是登陆过 
						as.setLiberalarts("4");
					if(as.getLiberalarts().equals("3"))   //如果填写过基础数据状态表
						as.setLiberalarts("5");
				}
				if("1103".equals(subject) ){ //如果是整体申报
					if(as.getWholeDeclaration().equals("2"))  //如果是登陆过 
						as.setWholeDeclaration("4");
					if(as.getWholeDeclaration().equals("3"))   //如果填写过基础数据状态表
						as.setWholeDeclaration("5");
				}
			}
			authSchoolService.updateAuthSchoolInfo(as);
			return toResponse(null,ReturnCode.SUCCESS,"新增成功!");
		}catch(Exception e){
			log.error(e);
			return toResponse(null,ReturnCode.FAIL,"新增失败!");
		}
	}
	
	/**
	 * 根据当前用户查询已填报的学生信息
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryStudentDataBySchoolId" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryStudentDataBySchoolId(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(null==sid){
			return toResponse(null,ReturnCode.NOLOGIN,"未登陆");
		}
		String subject = (String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);//学科

		StudentData sd = new StudentData();
		AuthStudentData asd = authStudentDataService.queryStudentDataBySchoolId(sid, subject);
		if(asd!=null){
			sd.setAuthStudentData(asd);
			String stuId = asd.getPid();
			List<AuthStudentSubjectMap> list = authStudentDataService.queryStudentSubjectByStuId(stuId);
			if(list.size()>0)
				sd.setAuthStudentSubjectMap(list);
		}
		return toResponse(sd,ReturnCode.SUCCESS,"查询成功!");
			
	}
	/**
	 * 修改学生表
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/updateStudentData" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryStudentData(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(null==sid){
			return toResponse(null,ReturnCode.NOLOGIN,"未登陆");
		}
		String subject = (String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);//学科
		try {
			AuthStudentData asd = authStudentDataService.queryStudentDataBySchoolId(sid, subject);
			if(null==asd){
				asd=new AuthStudentData();
			}
			String graduatedStudents = map.get("graduatedStudents"); //本专业已毕业学生数
			String graduatedUndergraduateMajor = map.get("graduatedUndergraduateMajor");
			String graduatedSpecializedSubject = map.get("graduatedSpecializedSubject");
			String schoolAllStudents = map.get("schoolAllStudents");//全校（院）在校学生数
			String allUndergraduateMajor = map.get("allUndergraduateMajor");
			String allSpecializedSubject = map.get("allSpecializedSubject");
			String inschoolStudents = map.get("inschoolStudents");//本专业在校学生数
			String inschoolUndergraduateMajor = map.get("inschoolUndergraduateMajor");
			String inschoolSpecializedSubject = map.get("inschoolSpecializedSubject");
			String inschoolboy = map.get("inschoolboy");
			String inschoolgirl = map.get("inschoolgirl");
			String teacherQualificationCertificate = map.get("teacherQualificationCertificate");//上年应届生获得相关教师资格证书比例
			String relevantEducationalInstitutions = map.get("relevantEducationalInstitutions");//上年毕业生到相关教育机构初次就业率
			String employingUnits = map.get("employingUnits");//上年毕业生跟踪调查用人单位满意率
			String subjectname = map.get("subjectname");
			String grade = map.get("grade");
			String gradeNumOne = map.get("gradeNumOne");
			String gradeNumTwo = map.get("gradeNumTwo");
			String gradeNumThree = map.get("gradeNumThree");
			String gradeNumFour = map.get("gradeNumFour");
			String wholeNum = map.get("wholeNum");
			String remark1 = map.get("remark1");
			String remark2 = map.get("remark2");
			String remark3 = map.get("remark3");
			String remark4 = map.get("remark4");
			String remark5 = map.get("remark5");
			String remark6 = map.get("remark6");
			String remark7 = map.get("remark7");
			String remark8 = map.get("remark8");
			String remark9 = map.get("remark9");
			String remark10 = map.get("remark10");
			String remark11 = map.get("remark11");
			String remark12 = map.get("remark12");
			String remark13 = map.get("remark13");
			String remark14 = map.get("remark14");
			
			String stuId = asd.getPid();
			if(graduatedStudents!=null && StringUtils.isNotBlank(graduatedStudents))
				asd.setGraduatedStudents(graduatedStudents);
			if(graduatedUndergraduateMajor!=null && StringUtils.isNotBlank(graduatedUndergraduateMajor))
				asd.setGraduatedUndergraduateMajor(graduatedUndergraduateMajor);
			if(graduatedSpecializedSubject!=null && StringUtils.isNotBlank(graduatedSpecializedSubject))
				asd.setGraduatedSpecializedSubject(graduatedSpecializedSubject);
			if(schoolAllStudents!=null && StringUtils.isNotBlank(schoolAllStudents))
				asd.setSchoolAllStudents(schoolAllStudents);
			if(allUndergraduateMajor!=null && StringUtils.isNotBlank(allUndergraduateMajor))
				asd.setAllUndergraduateMajor(allUndergraduateMajor);
			if(allSpecializedSubject!=null && StringUtils.isNotBlank(allSpecializedSubject))
				asd.setAllSpecializedSubject(allSpecializedSubject);
			if(inschoolStudents!=null && StringUtils.isNotBlank(inschoolStudents))
				asd.setInschoolStudents(inschoolStudents);
			if(inschoolUndergraduateMajor!=null && StringUtils.isNotBlank(inschoolUndergraduateMajor))
				asd.setInschoolUndergraduateMajor(inschoolUndergraduateMajor);
			if(inschoolSpecializedSubject!=null && StringUtils.isNotBlank(inschoolSpecializedSubject))
				asd.setInschoolSpecializedSubject(inschoolSpecializedSubject);
			if(inschoolboy!=null && StringUtils.isNotBlank(inschoolboy))
				asd.setInschoolboy(inschoolboy);
			if(inschoolgirl!=null && StringUtils.isNotBlank(inschoolgirl))
				asd.setInschoolgirl(inschoolgirl);
			if(teacherQualificationCertificate!=null && StringUtils.isNotBlank(teacherQualificationCertificate))
				asd.setTeacherQualificationCertificate(teacherQualificationCertificate);
			if(relevantEducationalInstitutions!=null && StringUtils.isNotBlank(relevantEducationalInstitutions))
				asd.setRelevantEducationalInstitutions(relevantEducationalInstitutions);
			if(employingUnits!=null && StringUtils.isNotBlank(employingUnits))
				asd.setEmployingUnits(employingUnits);
			String remark = remark1+"#"+remark2+"#"+remark3+"#"+remark4+"#"+remark5+"#"+remark6+"#"+remark7+"#"+remark8+"#"+remark9+"#"+remark10+"#"+remark11+"#"+remark12+"#"+remark13+"#"+remark14;
			asd.setRemark(remark);
			asd.setStatus("1");
			authStudentDataService.insertAuthStudentData(asd);
			//删除 TODO 
			
			authStudentDataService.deleteAuthStudentSubject(stuId);
			if(null!=asd.getPid()){
				String[] subjectnameList = subjectname.split(",");
				String[] gradeNumOneList = gradeNumOne.split(",");
				String[] gradeNumTwoList = gradeNumTwo.split(",");
				String[] gradeNumThreeList = gradeNumThree.split(",");
				String[] gradeNumFourList = gradeNumFour.split(",");
				String[] wholeNumList = wholeNum.split(",");
				String nowTime= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				
				for(int i = 0;i<subjectnameList.length;i++){
					AuthStudentSubjectMap assm = new AuthStudentSubjectMap();
					assm.setCreatetime(nowTime);
					assm.setGradeNumOne(gradeNumOneList[i]);
					assm.setGradeNumTwo(gradeNumTwoList[i]);
					assm.setGradeNumThree(gradeNumThreeList[i]);
					assm.setGradeNumFour(gradeNumFourList[i]);
					assm.setSdid(asd.getPid());
					assm.setSubjectname(subjectnameList[i]);
					assm.setWholeNum(wholeNumList[i]);
					authStudentDataService.insertAuthStudentSubjectMap(assm);
				}
				 List<AuthStudentSubjectMap> as = authStudentDataService.queryStudentSubjectByStuId(subjectname);
				 if(null!=as){
					 return toResponse(as,ReturnCode.SUCCESS,"查询年级表成功!");
				 }
			}
			return toResponse(asd,ReturnCode.SUCCESS,"修改学生表成功!");
		} catch (Exception e) {
			log.info("修改学生表失败!", e);
			return toResponse(null,ReturnCode.FAIL,"修改学生表失败!");
		}
		
	}
	/***********************************************************************************************************/
	/**
	 * 根据当前用户查询已填报的学生信息
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryStudentDataBySchoolId2" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryStudentDataBySchoolId2(HttpServletRequest request,@RequestParam String schoolid) throws SQLException{
		StudentData sd = new StudentData();
		AuthStudentData asd = authStudentDataService.queryStudentDataBySchoolId(schoolid, null);
		if(null!=asd){
			sd.setAuthStudentData(asd);
			String stuId = asd.getPid();
			List<AuthStudentSubjectMap> list = authStudentDataService.queryStudentSubjectByStuId(stuId);
			if(list.size()>0)
			sd.setAuthStudentSubjectMap(list);
		}else{
			asd = new AuthStudentData();
			sd.setAuthStudentData(asd);
		}
		return toResponse(sd,ReturnCode.SUCCESS,"查询学生数据成功!");
			
	}
	
}
